import router from "@/router/index";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {getToken} from "@/utils/auth";
import {store} from "@/store";
import {getLoginInfoById} from "@/api/login";

// NProgress.configure({showSpinner: false})

//路由白名单
const whiteList = ['/login']

router.beforeEach(async (to, from, next) => {
    NProgress.start()
    //已经登录
    if (getToken()) {
        if (to.path === '/login') {
            next()
        } else {
            if (store.state.account.permissions.length === 0) {
                //  获取个人信息
                let accountInfo = store.state.account.accountInfo
                await getLoginInfoById(accountInfo.id, getToken()).then(res => {
                    store.commit('SET_PERMISSION', res.data.buttonPermissions)
                    next()
                })
            } else {
                next()
            }
        }
    } else {
        // 没有token
        //TODO:判断是否存在白名单，可以直接访问
        next()
        // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
    }
})

router.afterEach(() => {
    NProgress.done()
})
